<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 角色管理控制器
 * @package App\Http\Controllers\Admin
 */
class SysRoleController extends BaseController
{
    private $roleSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->roleSer = new \App\Services\SysRoleService();
    }

    /**
     * 角色列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $roleList = $this->roleSer->getRoleList();
            foreach ($roleList as $k => $v){
                $v['text'] = $v['display_name'];
                $v['icon'] = 'icon iconfont icon-role';
                $v['tags'] = ['排序：'.$v['order_num']];
                $roleList[$k] = $v;
            }

            //树结构菜单列表
            $tree = new \App\Services\TreeService('role_id', 'parent_id', 'nodes');
            $tree->load($roleList);
            $treeMenuList = $tree->deepTree();
            $data['treeList'] = $treeMenuList ? $treeMenuList : [];
            return $data;
        }
        //权限数据
        $permSer = new \App\Services\SysPermissionService();
        $permList = $permSer->getChoiceList();
        if ($permList){
            foreach ($permList as $k => $v){
                $v['is_opentree'] = true;
                $permList[$k] = $v;
            }
        }
        //树结构权限列表列表
        $tree          = new \App\Services\TreeService('perm_id', 'parent_id', 'children');
        $tree->nameKey = 'display_name';
        $tree->load($permList);
        $this->viewData['permList'] = $tree->makeJsTreeFormat();

        return view('admin.sysrole.index', $this->viewData);
    }
    /**
     * 保存数据
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminSysRolePost $request)
    {
        $roleId = $request->input('role_id', 0);
        try {
            $rs = $this->roleSer->saveData($request);
            if (!$rs) {
                throw new \Exception(($roleId ? '编辑' : '添加') . '角色失败');
            }
            return $this->showMsg('success', ($roleId ? '编辑' : '添加') . '成功', 'admin.sysrole.index');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 保存角色权限
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function savePermission(Request $request)
    {
        try {
            $roleId = (int)$request->input('role_id', 0);
            if (!$roleId) {
                throw new \Exception('请选择要设置权限的角色');
            }
            $permIds = (array)$request->input('permIds', []);
            $rs = $this->roleSer->savePerm($roleId, $permIds);
            if ($rs) {
                return $this->showMsg('success', '设置用户组权限成功','admin.sysrole.index');
            }
            throw new \Exception('设置用户组权限失败');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }

    /**
     * 删除角色
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $id = $request->input('id', 0);
            if (!$id) {
                throw new \Exception('请指定要删除的用户组');
            }
            $rs = $this->roleSer->del([$id]);
            if (!$rs) {
                throw new \Exception('删除失败');
            }
            return $this->showMsg('success', '删除成功');
        } catch (\Exception $e) {
            return $this->showMsg('error', $e->getMessage());
        }
    }
}
